package com.curluin.approximateShapes;

import java.util.Vector;

import android.util.Log;

import com.curluin.math.MyVector;
import com.curluin.scene.Node;

public class Circle extends ApproximateShape {

	
	protected static String LOG_TAG="Circle";
	/**
	 * Three dimensiononal vector defining the center of the circle 
	 */
	private MyVector center;
	
	/**
	 * radius of the circle
	 */
	private float radius;
	
	
	
	public Circle(Node node) {
		radius=(float) (Math.max(Math.abs(node.outerSize.get(0)),Math.abs(node.outerSize.get(2)))/2.0);
		this.center = node.getCenter();
		Log.w(LOG_TAG,"center:"+center+" radius:"+radius+" size:"+node.outerSize);
	}
	/**
	 * Computes all z-coordinates of the shape lying on the specified x-coordinate,
	 * the line between two returned vectors is occupied by the shape (even=start points,uneven=end points).
	 * @param xCoordinate
	 * @return vectors, each two define a line
	 */
	private Vector<MyVector> computeSingleXScan(float xCoordinate) {
		Vector<MyVector> res= new Vector<MyVector>();
		float diffX=center.get(0)-xCoordinate;
		float centerZ=center.get(2);
		float knownTerm= diffX*diffX+centerZ*centerZ-radius*radius;
		float sqrtTerm=(float) Math.sqrt(centerZ*centerZ-knownTerm);
		MyVector result1=MyVector.makeVector3(xCoordinate,center.get(1),cutZBackToModelSpace(centerZ-sqrtTerm));
		MyVector result2=MyVector.makeVector3(xCoordinate,center.get(1),cutZBackToModelSpace(centerZ+sqrtTerm));
		res.add(result1);
		res.add(result2);
		return res;
	}

	
	@Override
	protected Vector<MyVector> initializeScanData(float sampleRate) {

		Vector<MyVector> res = new Vector<MyVector>();
		float xmin=center.get(0)-radius;
		float xmax=center.get(0)+radius;
		while(xmin<xmax){
			res.addAll(computeSingleXScan(xmin));
			xmin+=sampleRate;
		}
		res.addAll(computeSingleXScan(xmax));
		scanData=res;
		return res;
	}

	
	
	/* (non-Javadoc)
	 * @see com.curluin.approximateShapes.ApproximateShape#translateScanData(com.curluin.math.MyVector)
	 */
	@Override
	public Vector<MyVector> translateScanData(MyVector translation) {
		center=center.add(translation);
		return super.translateScanData(translation);
	}
	
	

}
